//
//  JTLabel.swift
//  TestUIKit
//
//  Created by Kiyo on 2021/4/9.
//

import UIKit

public class JTLabel: UILabel {

    private var longGestureRecognizer: UILongPressGestureRecognizer?
    
    /// 长按弹出的复制弹窗文案
    public var menuItemTitle: String = "Copy"
    
    /// 内容间距
    public var contentEdgeInsets: UIEdgeInsets = .zero {
        didSet {
            setNeedsLayout()
        }
    }

    /// 是否支持长按复制
    public var canPerformCopyAction: Bool = false {
        didSet {
            if canPerformCopyAction {
                let ges = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPressGestureRecognizer(_:)))
                longGestureRecognizer = ges
                addGestureRecognizer(ges)
            }
            else {
                if let ges = longGestureRecognizer {
                    removeGestureRecognizer(ges)
                }
                longGestureRecognizer = nil
            }
            isUserInteractionEnabled = canPerformCopyAction
        }
    }
    
    
    
    
    public override func sizeThatFits(_ size: CGSize) -> CGSize {
        
        var tempSize = super.sizeThatFits(CGSize(width: size.width - contentEdgeInsets.horizontalValue,
                                                 height: size.height - contentEdgeInsets.verticalValue))
        
        tempSize.width += contentEdgeInsets.horizontalValue
        tempSize.height += contentEdgeInsets.verticalValue
        return tempSize
    }
    
    public override var intrinsicContentSize: CGSize {
        
        var mWidth: CGFloat = preferredMaxLayoutWidth
        
        if (mWidth <= 0) {
            mWidth = CGFloat.greatestFiniteMagnitude
        }
        return sizeThatFits(CGSize(width: mWidth, height: CGFloat.greatestFiniteMagnitude))
    }
    
    public override func drawText(in rect: CGRect) {
        
        var tempRect = rect.inset(by: contentEdgeInsets)
        
        if numberOfLines == 1 {
            if lineBreakMode == .byWordWrapping || lineBreakMode == .byCharWrapping {
                tempRect.size.height = tempRect.height + contentEdgeInsets.top * 2
            }
        }
        super.drawText(in: tempRect)
    }
    
    public override var canBecomeFirstResponder: Bool {
        return canPerformCopyAction
    }
    
    public override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        
        guard canBecomeFirstResponder else { return false }
        
        return action == #selector(copyString(_:))
    }
    
    //MARK:- Action
    @objc
    private func handleLongPressGestureRecognizer(_ ges: UIGestureRecognizer) {
        
        guard canPerformCopyAction else { return }
        
        switch ges.state {
        case .began:
            becomeFirstResponder()
            let menuController = UIMenuController.shared
            let copyMenuItem = UIMenuItem(title: menuItemTitle, action: #selector(copyString(_:)))
            menuController.menuItems = [copyMenuItem]
            menuController.setTargetRect(frame, in: superview!)
            menuController.setMenuVisible(true, animated: true)
        default:
            break
        }
    }
    
    @objc
    private func copyString(_ sender: Any) {
        
        let pasteboard = UIPasteboard()
        pasteboard.string = text
    }
}
